<!--
Copyright 2003 Cordys R&D B.V. 

This file is part of the Cordys SAP Connector. 

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
	Project		: Cordys BAC
	File Name	: sap.htm
	Description : This page displays the properties of the SAP Application Connector.
	Author : Vijaya Gopal
-->
<html xmlns:eibus="xmlns:eibus" onapplicationready="initialize()">
<head>
	<script src="/cordys/wcp/application.js"></script>
	<style>
		input{font-size:11; font-family:verdana;}
		label{font-size:11; font-family:verdana; }
		eibus:\select{font-size:11; font-family:verdana;}		
		.PASSWORD { behavior:url('/cordys/wcp/admin/behavior/base64Encoder.htc') }
	</style>
</head>	
<script src="/cordys/wcp/admin/behavior/applicationconnector.js">
</script>
<script language="JScript">
	/**
	 * Holds the namespace for the configuration XML.
	 */
	var NS_SAP_CONFIG = "http://sapconnector.cordys.com/2.0/configuration";
	
	// To validate the details provided
	function fnValidate()
	{
		var allInputElements = table_details.getElementsByTagName("input");
		var isJCoChecked = radio_JCo.checked;
		for(var i = 0; i < allInputElements.length; i++)
		{
			var element = allInputElements[i];
			var reqFor = element.reqFor;
			if(element.disabled == true || element.readonly == true)
				continue;
			if(isJCoChecked)
			{
				if(reqFor == "Both"||reqFor == "JCo" )
				{
					elementID = element.id;
					if(!(elementID == "input_idocServers" || elementID == "input_gateway_kstype" || elementID == "input_progid_ksprovider"))
						if(!fnValidateAnElement(element))
							return false;					
				}	
			}
			else
			{
				if(reqFor == "Both" ||reqFor == "BC")
					if(!fnValidateAnElement(element))
						return false;
			}
		}
		if(isJCoChecked)
		{
			// If the user doesn't want to configure IDOC servers			
			if(!(input_idocServers.value == "" ||parseInt(input_idocServers.value) == 0))
			{
				if(isValidNumber(input_idocServers))
				{
					if(!isValidText(input_gateway_kstype))
					{
						alert(input_gateway_kstype.msg);
						input_gateway_kstype.focus();
						return false;
					}
					if(!isValidText(input_progid_ksprovider))
					{
						alert(input_progid_ksprovider.msg);
						input_progid_ksprovider.focus();
						return false;
					}
				}
				else
				{
					alert(input_idocServers.msg);
					input_idocServers.focus();
					return false;
				}
			}
		}		
		return true;
	}
	// Validates the input element passed based on the fieldType
	function fnValidateAnElement(element)
	{
		var fieldType = element.fieldType;
		var isValid = true;
		if(fieldType == "integer")
		{
			isValid = isValidNumber(element);
		}
		else if(fieldType == "text")
		{
			isValid = isValidText(element);
		}
		else if(fieldType == "path")
		{
			isValid = checkFolderPathForValidity(element.value);
		}
		if(isValid == false)
		{
			alert(element.msg);
			element.select();
			return false;
		}
		return true;
	}
	function checkFolderPathForValidity(path)
	{
		//Check for double slashes
		var regEx_doubleSlash = /\\\\/g;
		if(regEx_doubleSlash.test(path))
			return false;
		// Check for validity of path i.e if there are any characters which are not allowed.
		//Characters /,\,:,*,?,",<,>,| are not allowed in folder names or file names
		var regEx_folderPath = /[a-zA-Z]:\\[^\/:\*\?"<>\|]{0,260}$/;
		if(!regEx_folderPath.test(path))
			return false;
		return true;
	}
	//To check if the given textbox contains valid text,Also works for TextArea,Also checks for spaces
	function isValidText(objElement)
	{
		var value=objElement.value;
		if(value==null || value=="")
			return false;
		var regExSpace = /\s/g;
		if(regExSpace.test(value))
			return false;
		return true;
	}
	// To check if the given textbox contains a valid number or not
	function isValidNumber(objElement)
	{
		if(!isValidText(objElement)) return false;
		var value = objElement.value;
		var regExInteger = /[^0-9]/g;
		if(regExInteger.test(value))
			return false;
		return true;
	}
	/**
	 * This method creates the configuration XML based on the given inputs.
	 *
	 * @param configurationNode The parent configuration node.
	 */
	function createConnectorConfiguration(configurationNode)
	{
		if(!fnValidate())
		{
			return false;
		}
			
		configXmlDocument = configurationNode.ownerDocument;
		configurationNode.setAttribute("implementation", "com.eibus.applicationconnector.sap.SAPMapper");

		//We need to create the XML using a namespace so that the XML is read properly.
		//Also we need to take browser independence into account.
		var connectionNode = cordys.createElementNS(configXmlDocument, NS_SAP_CONFIG, "connection");
		cordys.appendXMLNode(connectionNode, configurationNode);

		createElementWithParentNS(connectionNode, "sapserver", input_sapserver.value);

		createElementWithParentNS(connectionNode, "fallbackToSystemUser", txtFallback.checked ? "true" : "false");

		var value = (typeof(txtAllwaysUseMappedUsername) != "undefined" ? txtAllwaysUseMappedUsername.checked : false);
		createElementWithParentNS(connectionNode, "alwaysUseMappedUser", value ? "true" : "false");
		
		if(radio_JCo.checked)
		{
			createElementWithParentNS(connectionNode, "cachedirectory", input_cachedir_kslocation.value);			

			var jcoNode = createElementWithParentNS(connectionNode, "jco", null);
			
			createElementWithParentNS(jcoNode, "client", input_BCPor_Client.value);
			createElementWithParentNS(jcoNode, "sysnum", input_BCSer_SysNum.value);
			createElementWithParentNS(jcoNode, "language", input_RFCURL_lang.value);
			createElementWithParentNS(jcoNode, "userid", input_userid_cachedir.value);
			
			var encodedPassword = input_user_password.encode(input_user_password.value);			
			createElementWithParentNS(jcoNode, "password", encodedPassword);
			createElementWithParentNS(jcoNode, "idocservers", input_idocServers.value);
			createElementWithParentNS(jcoNode, "gwserv", input_gateway_kstype.value);
			createElementWithParentNS(jcoNode, "progID", input_progid_ksprovider.value);

			createElementWithParentNS(jcoNode, "maxCons", input_maxcons.value);
		}
		else
		{
			createElementWithParentNS(connectionNode, "cachedirectory", input_userid_cachedir.value);
						
			var bcNode = createElementWithParentNS(connectionNode, "bc", null);
			
			createElementWithParentNS(bcNode, "bcserver", input_BCSer_SysNum.value);
			createElementWithParentNS(bcNode, "bcport", input_BCPor_Client.value);

			var protocol = select_protocol.value;
			createElementWithParentNS(bcNode, "protocol", protocol);
			if(protocol == "https")
			{
				createElementWithParentNS(bcNode, "keystoreloc", input_cachedir_kslocation.value);
				var encodedPassword = input_user_password.encode(input_kspassword.value);				
				createElementWithParentNS(bcNode, "keystorepassword", encodedPassword);
				createElementWithParentNS(bcNode, "type", input_gateway_kstype.value);
				createElementWithParentNS(bcNode, "provider", input_progid_ksprovider.value);
			}

			createElementWithParentNS(bcNode, "bapiinvokeurl", input_BAPIURL.value);
			createElementWithParentNS(bcNode, "rfcinvokeurl", input_RFCURL_lang.value);
			createElementWithParentNS(bcNode, "idocinvokeurl", input_IDOCURL.value);
		}
		
		return true;
	}
	
	/**
	 * This method reads the XML configuration and fills the UI with the proper data.
	 */
	function fillInPropertyScreen(configurationNode)
	{
		//Initialize the namespace mapping		
		var namespaces = new Array();
		namespaces["ns"] = NS_SAP_CONFIG;

		var connectionNode = cordys.selectXMLNode(configurationNode, "//ns:connection", namespaces);
		if (connectionNode == null)
		{
			return;
		}

		//Get the SAP server
		var sapServer = cordys.getNodeText(connectionNode, "ns:sapserver", "", namespaces);
		input_sapserver.value = sapServer;

		//The 2 check boxes
		var temp = cordys.getNodeText(connectionNode, "ns:fallbackToSystemUser", "", namespaces);
		txtFallback.checked = (temp == "true" ? true : false);
		
		temp = cordys.getNodeText(connectionNode, "ns:alwaysUseMappedUser", "", namespaces);
		if (typeof(txtAllwaysUseMappedUsername) != "undefined")
		{
			txtAllwaysUseMappedUsername.checked = (temp == "true" ? true : false);
		}

		//Get the jco middleware details
		var jcoNode = cordys.selectXMLNode(connectionNode, "ns:jco", namespaces);
		var bcNode = cordys.selectXMLNode(connectionNode, "ns:bc", namespaces);

		if (jcoNode != null)
		{
			radio_JCo.checked = true;
		}
		else
		{
			radio_BC.checked = true ;
		}
		
		//Show the appropiate elements and proper captions.
		fnShowAppropriateElements();
		
		//Get the cache directory
		var cacheDirectory = cordys.getNodeText(connectionNode, "ns:cachedirectory", "", namespaces);
				
		if(radio_JCo.checked == true)
		{
			input_cachedir_kslocation.value = cacheDirectory;
			
			var client = cordys.getNodeText(jcoNode, "ns:client", "", namespaces);
			input_BCPor_Client.value = client;
			
			var sysnum = cordys.getNodeText(jcoNode, "ns:sysnum", "", namespaces);
			input_BCSer_SysNum.value = sysnum;
			
			var language = cordys.getNodeText(jcoNode, "ns:language", "", namespaces);
			input_RFCURL_lang.value = language;
			
			var maxConsNode = cordys.getNodeText(jcoNode, "ns:maxCons", "", namespaces);
			input_maxcons.value = maxConsNode;
						
			var userid = cordys.getNodeText(jcoNode, "ns:userid", "", namespaces);
			input_userid_cachedir.value = userid;
			
			var encodedPassword = cordys.getNodeText(jcoNode, "ns:password", "", namespaces);
			if (encodedPassword != "")
			{
				encodedPassword = input_user_password.decode(encodedPassword);
			}
			input_user_password.value = encodedPassword;
						
			var idocservers = cordys.getNodeText(jcoNode, "ns:idocservers", "", namespaces);
			input_idocServers.value = idocservers;
			
			var gwserv = cordys.getNodeText(jcoNode, "ns:gwserv", "", namespaces);
			input_gateway_kstype.value = gwserv;
			
			var progID = cordys.getNodeText(jcoNode, "ns:progID", "", namespaces);
			input_progid_ksprovider.value = progID;
		}
		else
		{
			input_userid_cachedir.value = cacheDirectory;
			
			var bcServer = cordys.getNodeText(bcNode, "ns:bcserver", "", namespaces);
			input_BCSer_SysNum.value = bcServer;
			
			var bcPort = cordys.getNodeText(bcNode, "ns:bcport", "", namespaces);
			input_BCPor_Client.value = bcPort;
			
			var protocol = cordys.getNodeText(bcNode, "ns:protocol", "", namespaces);
			if(protocol == "https")
			{
				select_protocol.value = "https" ;
				fnEnableDisableKeyStore();
				
				var keyStoreLoc = cordys.getNodeText(bcNode, "ns:keystoreloc", "", namespaces);
				input_cachedir_kslocation.value = keyStoreLoc;
				
				var keyStorePassword = cordys.getNodeText(bcNode, "ns:keystorepassword", "", namespaces);
				var decodedPassword = keyStorePassword ? input_user_password.decode(keyStorePassword) : "" ;				
				input_kspassword.value = decodedPassword;
				
				var type = cordys.getNodeText(bcNode, "ns:type", "", namespaces);
				input_gateway_kstype.value = type;
				
				var provider = cordys.getNodeText(bcNode, "ns:provider", "", namespaces);
				input_progid_ksprovider.value = provider;
			}
			
			var bapiURLNode = cordys.getNodeText(bcNode, "ns:bapiinvokeurl", "", namespaces);
			input_BAPIURL.value = bapiURLNode;
			
			var rfcURLNode = cordys.getNodeText(bcNode, "ns:rfcinvokeurl", "", namespaces);
			input_RFCURL_lang.value = rfcURLNode;
			
			var idocURLNode = cordys.getNodeText(bcNode, "ns:idocinvokeurl", "", namespaces);
			input_IDOCURL.value = idocURLNode;
		}
	}
	
	// To show appropriate input elements based on the radio button checked.
	function fnShowAppropriateElements()
	{
		if(radio_JCo.checked)
		{
			label_SAPServer.innerText = "SAP Application Server" ;
			label_idocservers_protocol.innerText = "Number of IDOC Servers";
			input_idocServers.style.display = "";
			div_select_protocol.style.display = "none";
			label_BCSer_SysNum.innerText = "System Number" ;
			input_BCSer_SysNum.msg = "Enter SAP System Number.";
			input_BCSer_SysNum.fieldType = "integer" ;
			label_gateway_kstype.innerText = "SAP Gateway" ;
			label_gateway_kstype.title="SAP Gateway service number" ;			
			input_gateway_kstype.title="SAP Gateway service number" ;
			input_gateway_kstype.value = "";
			input_gateway_kstype.readOnly = false ;
			input_gateway_kstype.style.backgroundColor = "";
			input_gateway_kstype.msg = "Enter SAP Gateway service number." ;
			label_BCPor_Client.innerText = "SAP Client" ;
			input_BCPor_Client.msg = "Enter SAP Client number." ;			
			input_progid_ksprovider.value = "" ;
			input_progid_ksprovider.readOnly = false ;
			input_progid_ksprovider.style.backgroundColor = "" ;
			input_progid_ksprovider.msg = "Enter Program ID." ;			
			label_progid_ksprovider.innerText = "Program ID" ;
			label_userid_cachedir.innerText = "SAP User ID" ;
			input_userid_cachedir.msg = "Enter SAP User ID." ;
			input_userid_cachedir.fieldType = "text" ;
			label_cachedir_kslocation.innerText = "Cache Directory" ;
			input_cachedir_kslocation.disabled = false;
			input_cachedir_kslocation.msg = "Enter metadata cache directory." ;
			label_BAPIURL_password.innerText = "Password" ;			
			input_user_password.style.display = "" ;
			input_BAPIURL.style.display = "none" ;		
			label_maxcons_kspassword.innerText = "Maximum Connections" ;			
			input_maxcons.style.display = "" ;
			input_kspassword.style.display = "none" ;			
			label_RFCURL_lang.innerText = "Language" ;
			label_RFCURL_lang.title = "SAP Logon Language";
			input_RFCURL_lang.title = "SAP Logon Language";
			input_RFCURL_lang.msg = "Enter SAP Logon language." ;
			input_RFCURL_lang.value = "EN" ;			
			tr_row7.style.visibility = "hidden" ;		
		}
		else
		{
			label_SAPServer.innerText = "SAP Server Name" ;
			label_idocservers_protocol.innerText = "Protocol";
			input_idocServers.style.display = "none";
			div_select_protocol.style.display = "";
			label_BCSer_SysNum.innerText = "BC Server" ;
			input_BCSer_SysNum.msg = "Enter BC Server Name.";
			input_BCSer_SysNum.fieldType = "text" ;
			label_gateway_kstype.innerText = "KeyStore Type" ;
			label_gateway_kstype.title = "" ;
			input_gateway_kstype.title = "" ;			
			input_gateway_kstype.value = "JKS";
			input_gateway_kstype.readOnly = true ;
			input_gateway_kstype.style.backgroundColor = "transparent";
			input_gateway_kstype.msg = "" ;
			label_BCPor_Client.innerText = "BC Port" ;
			input_BCPor_Client.msg = "Enter BC Port number." ;
			label_progid_ksprovider.innerText = "KeyStore Provide" ;			
			input_progid_ksprovider.value = "SUN" ;
			input_progid_ksprovider.readOnly = true ;
			input_progid_ksprovider.style.backgroundColor = "transparent" ;
			input_progid_ksprovider.msg = "" ;
			label_userid_cachedir.innerText = "Cache Directory" ;
			input_userid_cachedir.msg = "Enter metadata cache directory.";
			input_userid_cachedir.fieldType = "path" ;
			label_cachedir_kslocation.innerText = "KeyStore Location" ;
			input_cachedir_kslocation.disabled = true;
			input_cachedir_kslocation.msg = "Enter key store location.";
			label_BAPIURL_password.innerText = "BAPI Service URL" ;
			input_user_password.style.display = "none" ;
			input_BAPIURL.style.display = "" ;			
			label_maxcons_kspassword.innerText = "KeyStore Password" ;
			input_maxcons.style.display = "none" ;
			input_kspassword.style.display = "" ;			
			label_RFCURL_lang.innerText = "RFC Service URL" ;
			label_RFCURL_lang.title = "";
			input_RFCURL_lang.title = "";
			input_RFCURL_lang.msg = "Enter the RFC Service invoke URL." ;
			input_RFCURL_lang.value = "invoke/sap.demo/handle_RFC_XML_POST" ;
			tr_row7.style.visibility = "visible" ;
			fnEnableDisableKeyStore();			
		}
	}
	// To enable or disable the key store detail elements based on the protocol
	function fnEnableDisableKeyStore()
	{
		if(select_protocol.value == "http")
		{
			input_cachedir_kslocation.disabled = true ;
			input_kspassword.disabled = true ;
		}
		else
		{
			input_cachedir_kslocation.disabled = false ;
			input_kspassword.disabled = false ;
		}
	}

	/**
	 * This method creates a new XML element using the namespace of the parent node.
	 *
	 * @param parent The parent XML node.
	 * @param name   The name of the new element.
	 * @param value  The value for the text element.
	 *
	 * @return The created element.
	 */
	function createElementWithParentNS(parent, name, value)
	{
		var returnValue = null;
		
		if (!(parent == null || typeof(parent.ownerDocument) == "undefined"))
		{
			var doc = parent.ownerDocument;
			var namespace = parent.namespaceURI;

			returnValue = cordys.createElementNS(doc, namespace, name);
			cordys.appendXMLNode(returnValue, parent);

			if (value != null)
			{
				cordys.setTextContent(returnValue, value);
			}
		}

		return returnValue;
	}  
</script>
<body  class="mainbody" style="border:1px solid darkgray;overflow:auto"  leftmargin = "0" topmargin = "0" >
	<div style="border-bottom:'0.1mm solid darkgray'; height:'10%'; width:'100%'">
		<input type="checkbox" id="txtFallback" style="width:5%" tabindex = "2">
		<label for="txtFallback">Fallback to system user if no usermapping is defined for user</label>
<!-- PGUSSOW: For now this option will not be shown, since it needs more change in the backend.
		<br>
		<input type="checkbox" id="txtAllwaysUseMappedUsername" style="width:5%" tabindex = "">
		<label for="txtAllwaysUseMappedUsername">Always use mapped username instead of system user</label>
 -->		
		<br>
		<input type = "radio" name = "radio_middleware" id = "radio_JCo" style="width:5%" onclick="fnShowAppropriateElements()" tabindex = "1" checked>
		<label for = "radio_JCo">JCo</label>
		<br>
		<input type = "radio" name = "radio_middleware" id= "radio_BC" style="width:5%" onclick="fnShowAppropriateElements()" tabindex = "1">
		<label for = "radio_BC">BC</label>
	</div>
	<!-- 89% so that scroll doesn't appear by default-->
	<table height = "89%" width = "100%" id = "table_details" cellspacing = "10">
		<tr>
			<td width = "50%">
				<label id = "label_SAPServer">SAP Application Server</label>
				<br>
				<input type = "text" class = "input" id = "input_sapserver" style = "width:'90%'" tabindex = "1" msg = "Enter SAP Server name." reqFor = "Both" fieldType = "text">
			</td>
			<td width = "50%">
				<label id="label_idocservers_protocol">Number of IDOC Servers</label>
				<br>
				<input type="text" class="input" id="input_idocServers" style = "width:90%" tabindex = "8" msg="Enter the number of IDOC Servers" reqFor = "JCo" fieldType = "integer">
				<div id="div_select_protocol" style="display:none" tabindex = "8">
					<eibus:select id = "select_protocol" class = "input" style = "width:90%" onchange = "fnEnableDisableKeyStore()">
						<div value = "http">HTTP</div>
						<div value = "https">HTTPS</div>
					</eibus:select>
				</div>				
			</td>
		</tr>
		<tr>
			<td width = "50%">
				<label id = "label_BCSer_SysNum">System Number</label>
				<br>
				<input type = "text" class = "input" id = "input_BCSer_SysNum" style = "width:'90%'" tabindex = "2" msg = "Enter SAP System Number." reqFor = "Both" fieldType = "integer">
			</td>
			<td width = "50%">
				<label id="label_gateway_kstype" title="SAP Gateway service number">SAP Gateway</label>
				<br>
				<input type = "text" class = "input" id = "input_gateway_kstype" style = "width:'90%'" title="SAP Gateway service number" reqFor = "Both" fieldType = "text" msg="Enter SAP Gateway service number." tabindex = "9">
			</td>
		</tr>
		<tr>
			<td width = "50%">
				<label id = "label_BCPor_Client">SAP Client</label>
				<br>
				<input type = "text" class = "input" id = "input_BCPor_Client" style = "width:'90%'" tabindex = "3" msg = "Enter SAP Client number." reqFor = "Both" fieldType = "integer">
			</td>
			<td width = "50%">
				<label id="label_progid_ksprovider">Program ID</label>
				<br>
				<input type = "text" class = "input" id = "input_progid_ksprovider" style = "width:'90%'" msg="Enter Program ID." reqFor = "Both" fieldType = "text" tabindex = "10">
			</td>
		</tr>
		<tr>
			<td width = "50%">
				<label id="label_userid_cachedir">SAP User ID</label>
				<br>
				<input type = "text" class = "input" id = "input_userid_cachedir" style = "width:'90%'" tabindex = "4" msg = "Enter SAP User ID." reqFor = "JCo" fieldType = "text">
			</td>
			<td width = "50%">
				<label id="label_cachedir_kslocation">Cache Directory</label>
				<br>
				<input type = "text" class = "input" id = "input_cachedir_kslocation" style = "width:'90%'" tabindex = "11" msg = "Enter metadata cache directory." reqFor = "None" fieldType = "path">
			</td>
		</tr>
		<tr>
			<td width = "50%">
				<label id = "label_BAPIURL_password">Password</label>
				<br>
				<input type = "password" class = "input PASSWORD" id = "input_user_password" style = "width:'90%'" tabindex = "5" reqFor = "JCo" msg="Enter password." fieldType = "text">
				<input type="text" class="input" id="input_BAPIURL" value="invoke/sap.bapi/handleBusXMLPost" style = "width:'90%';display:none" tabindex = "5" reqFor = "BC" fieldType = "text" msg="Enter the BAPI Service invoke URL.">			
			</td>
			<td width = "50%">
				<label id="label_maxcons_kspassword">Maximum Connections</label>
				<br>
				<input type = "text" class = "input" id = "input_maxcons" style = "width:'90%'" tabindex = "12" value="10" msg = "Enter the number of maximum connections." reqFor = "JCo" fieldType = "integer">
				<input type="password" class="input" id = "input_kspassword" style = "width:'90%';display:none" tabindex = "12" reqFor = "BC" disabled>
			</td>
		</tr>
		<tr>
			<td width = "50%">
				<label id = "label_RFCURL_lang" title = "SAP Logon Language">Language</label>
				<br>
				<input type = "text" class = "input" id = "input_RFCURL_lang" title = "SAP Logon Language" style = "width:'90%'" tabindex = "6" value = "EN" msg = "Enter SAP Logon language." reqFor = "Both" fieldType = "text">
			</td>
		</tr>
		<tr id = "tr_row7" style = "visibility:hidden">
			<td width = "50%">
				<label>IDOC Service URL</label>
				<br>
				<input type = "text" class = "input" id = "input_IDOCURL" style = "width:'90%'" tabindex = "7" value = "invoke/sap.bapi/handleBusXMLPost" msg = "Enter the IDOC Service invoke URL." reqFor = "BC" fieldType = "text">
			</td>
		</tr>
	</table>
</body>
</html>